﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace KZ400B
{
    public partial class AxleManager
    {
        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="axleName"></param>
        /// <returns></returns>
        public static bool Stop(string axleName)
        {
            if (!IsOpen)
            {
                return false;
            }
            //bool rst = false;
            string data = Protocol.Packet(
                Protocol.GetStopString(axleName)
                );
            return sendData(data);
            //try
            //{
            //    string msg = string.Empty;
            //    lock (s_spLocker)
            //    {
            //        //if (spLink.CanWrite)
            //        {
            //            spLink.Write(data);
            //            msg = spLink.ReadString(2);
            //            if (!string.IsNullOrEmpty(msg))
            //            {
            //                rst = handleNrmAck(FunctionCode.FCodeStop,axleName,msg, false)
            //                    == CommResultCode.Success
            //                    ;
            //            }
            //        }
            //    }
                
            //    if (OnAxleActionResponse != null)
            //    {
            //        OnAxleActionResponse(axleName
            //            , AxleCacheManager.getAxle(axleName)
            //            , AxleActions.Stop
            //            , rst
            //            , string.Format("[{0}] 轴 停止命令响应 : {1} {2} "
            //            , axleName, rst.ToString(), rst ? "-> 正在停止 ... " : string.Empty
            //            )
            //        );
            //    }
            //}
            //finally
            //{
            //    //setIsLocked(false);
            //}
            //return rst;
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="axleNameList"></param>
        public static bool Stop(List<string> axleNameList)
        {
            if (!IsOpen)
            {
                return false;
            }
            //bool rst = false;
            string data = Protocol.Packet(
                Protocol.GetStopString(axleNameList)
                );
            return sendData(data);
            //try
            //{
            //    string msg = string.Empty;
            //    lock (s_spLocker)
            //    {
            //        if (spLink.CanWrite)
            //        {
            //            spLink.Write(data);
            //            msg = spLink.ReadString();
            //        }
            //    }
            //    rst = handleNrmAck(FunctionCode.FCodeStop,axleNameList[0],msg)
            //        == CommResultCode.Success
            //        ;
            //    if (OnAxleActionResponse != null)
            //    {
            //        OnAxleActionResponse(Axle.AllAxleName
            //            , AxleCacheManager.getAxle(Axle.AllAxleName)
            //            , AxleActions.Stop
            //            , rst
            //            , string.Format("[{0}] 轴 停止命令响应 : {1} {2} "
            //            , Axle.AllAxleName, rst.ToString(), rst ? "-> 正在停止 ... " : string.Empty
            //            )
            //        );
            //    }
            //}
            //finally
            //{
            //    //setIsLocked(false);
            //}
            //return rst;
        }


        public static void OnStopRespond(ProtocolV2.UnpacketResult ur)
        {
            if (ur.Result
                    && ur.Action == AxleActions.StopRespond

                    )
            {
                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].OtherAxleStatus = AxleActions.StopRespond;
                    AxleCacheManager.AxleList[ur.AxleName].AxleStatus = AxleStatus.Running;
                }
            }
            if (!ur.Result)
            {
                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].OtherAxleStatus = AxleActions.StopError;
                    AxleCacheManager.AxleList[ur.AxleName].AxleStatus = AxleStatus.Stoped;
                }
            }

            setState(
                string.Format("[{0}] 轴 停止命令响应 : {1} {2} "
                        , ur.AxleName
                        , ur.Result ? "正确" : "错误"
                        , ur.Result ? "-> 停止运行中 ... " : string.Empty
                        )
            );
        }

        public static void OnStopComplete(ProtocolV2.UnpacketResult ur)
        {
            if (ur.Result
                && ur.Action == AxleActions.StopFinish

                )
            {

                AxleRunState.Instance[ur.AxleName] = false;
                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    //AxleManager.QueryPosition(ur.AxleName);
                    //Thread.Sleep(100);
                }

                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].OtherAxleStatus = AxleActions.StopFinish;
                }

                AxleCacheManager.AxleList[ur.AxleName].TargetParametersSetting.ResetRunState();

            }
            else
            {
                if (AxleCacheManager.AxleList.ContainsKey(ur.AxleName))
                {
                    AxleCacheManager.AxleList[ur.AxleName].OtherAxleStatus = AxleActions.StopError;
                }
            }
            AxleCacheManager.AxleList[ur.AxleName].AxleStatus = AxleStatus.Stoped;
            setState(
                string.Format("[{0}] 轴 停止命令运行结束 : {1}"
                        , ur.AxleName
                        , ur.Result ? "正确" : "错误"
                        )
            );
        }

    }
}
